package com.tcd.explore.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tcd.explore.pojo.SurveyLossInfo;
import com.tcd.explore.service.SurveyLossInfoService;
import com.tcd.outRisk.entity.ClaimMainInfo;
import com.tcd.service.CaseReportInfoservice;
import com.tcd.service.Claimmainservice;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 查勘定损信息表 前端控制器
 * </p>
 *
 * @author Jarle
 * @since 2021-05-25
 */
@RestController
@CrossOrigin
@RequestMapping("/generator")
public class SurveyLossInfoController {
    @Autowired
    SurveyLossInfoService survey;
    @Autowired
    Claimmainservice claimmainservice;
    @Autowired
    CaseReportInfoservice caseReportInfoservice;

    //查询所有的信息
    @RequestMapping("/surverylist")
    public Map<String, Object> list(int page, int pagesize) {
        Map<String, Object> map = new HashMap<>();
        List<SurveyLossInfo> list = survey.list();
        map.put("count", list.size());
        List<SurveyLossInfo> listsplice = survey.Surveysplice(page, pagesize);
        listsplice.forEach(item -> item.getClaim().setCases(caseReportInfoservice.caseReportone(item.getClaim().getCaseReportId() + "")));
        map.put("list", listsplice);
        return map;
    }

    @RequestMapping("/surveryadd")
    public boolean SurveyLossadd(@RequestBody SurveyLossInfo surveyLossInfo) {
        System.out.println(surveyLossInfo.toString());
        Long id = IdWorker.getId();
        String num = 438 + "" + id.intValue();
        if (num.indexOf('-') != -1) {
            num = num.substring(0, num.lastIndexOf("-")) + num.substring(num.lastIndexOf("-") + 1, num.length() - 1);
        }
        surveyLossInfo.setSurveyLossId(Long.valueOf(num.substring(0, 9)));
        return survey.save(surveyLossInfo);
    }

    @RequestMapping("/surverystatusedit")
    public boolean SurveyStatusEdit(@RequestBody SurveyLossInfo surveyLossInfo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("survey_loss_id", surveyLossInfo.getSurveyLossId());
        if (surveyLossInfo.getSurveystatus().equalsIgnoreCase("1")) {//如果为注销了
            //改变案件审核状态为撤案
            claimmainservice.Clainmainupdatestatus(surveyLossInfo.getClaimMainId() + "", 4 + "");
            //获得赔案实体
            ClaimMainInfo claim = claimmainservice.Clainmainone(surveyLossInfo.getClaimMainId() + "");
            //改变报案的状态；
            caseReportInfoservice.CaseReportInfoupdatestatus(claim.getCaseReportId() + "", 3 + "");
        } else if (surveyLossInfo.getSurveystatus().equalsIgnoreCase("3")) {//如果为受理了
            //改变案件编号
            claimmainservice.Clainmainupdatestatus(surveyLossInfo.getClaimMainId() + "", 2 + "");
        }
        return survey.update(surveyLossInfo, wrapper);
    }

    @RequestMapping("/surveryaccept")
    public Map<String, Object> Surveyaccept(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int pagesize) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("survey_status", 3);
        List<SurveyLossInfo> list = survey.list(wrapper);
        list.forEach(item -> item.setClaim(claimmainservice.getClaimmainone(item.getClaimMainId() + "s")));
        map.put("count", list.size());
        Page<SurveyLossInfo> page1 = PageHelper.startPage(page, pagesize);
        map.put("list", list);
        return map;
    }

    @RequestMapping("/surveryeditstatus")
    public boolean SurveyStatusEditstatus(String sid, String status, String claimid) {
        return survey.SurveyStatusEditstatus(sid, status);
    }

    @RequestMapping("/surverycount")
    public int Surcount(String Claimmainid) {
        return survey.Suryveycount(Claimmainid);
    }

}

